<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Gemini","version":"8.7.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="Uniapp 开发记录使用 Vue3 的 setup 语法糖配置跨域代理">
<meta property="og:type" content="article">
<meta property="og:title" content="Uniapp 开发记录">
<meta property="og:url" content="http://example.com/2023/01/26/00/index.html">
<meta property="og:site_name" content="Xiaolong&#39;s Blog">
<meta property="og:description" content="Uniapp 开发记录使用 Vue3 的 setup 语法糖配置跨域代理">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2023-02-14_14-56-23.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/Drwna/image//images/20230214162851.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/Drwna/image//images/GIF%202023-2-14%2017-10-47%20(2).gif">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/Drwna/image//images/20230214174116.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/Drwna/image//images/20230214181046.png">
<meta property="article:published_time" content="2023-01-25T16:00:00.000Z">
<meta property="article:modified_time" content="2023-02-20T09:21:53.033Z">
<meta property="article:author" content="Xiaolong">
<meta property="article:tag" content="uniapp">
<meta property="article:tag" content="vue3">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2023-02-14_14-56-23.png">


<link rel="canonical" href="http://example.com/2023/01/26/00/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"http://example.com/2023/01/26/00/","path":"2023/01/26/00/","title":"Uniapp 开发记录"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Uniapp 开发记录 | Xiaolong's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Xiaolong's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Uniapp-%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95"><span class="nav-number">1.</span> <span class="nav-text">Uniapp 开发记录</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8-Vue3-%E7%9A%84-setup-%E8%AF%AD%E6%B3%95%E7%B3%96"><span class="nav-number">1.1.</span> <span class="nav-text">使用 Vue3 的 setup 语法糖</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%85%8D%E7%BD%AE%E8%B7%A8%E5%9F%9F%E4%BB%A3%E7%90%86"><span class="nav-number">1.2.</span> <span class="nav-text">配置跨域代理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#vue2"><span class="nav-number">1.2.1.</span> <span class="nav-text">vue2</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vue3-%E6%97%A0ts"><span class="nav-number">1.2.2.</span> <span class="nav-text">vue3 (无ts)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#App-%E7%AB%AF%E6%97%A0%E8%B7%A8%E5%9F%9F%E9%97%AE%E9%A2%98%EF%BC%8C%E5%8F%AF%E4%BB%A5%E7%9B%B4%E6%8E%A5%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82"><span class="nav-number">1.2.3.</span> <span class="nav-text">App 端无跨域问题，可以直接发送请求</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BA%94%E7%94%A8%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="nav-number">1.3.</span> <span class="nav-text">应用生命周期</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9D%A1%E4%BB%B6%E7%BC%96%E8%AF%91"><span class="nav-number">1.4.</span> <span class="nav-text">条件编译</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%89%88%E6%9C%AC%E5%8D%87%E7%BA%A7%EF%BC%88%E6%95%B4%E5%8C%85%E6%9B%B4%E6%96%B0%EF%BC%89"><span class="nav-number">1.5.</span> <span class="nav-text">版本升级（整包更新）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%85%8D%E7%BD%AE%E5%BA%95%E9%83%A8%E5%AF%BC%E8%88%AA-tabbar"><span class="nav-number">1.6.</span> <span class="nav-text">配置底部导航 tabbar</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E9%A1%B6%E9%83%A8-top-bar"><span class="nav-number">1.7.</span> <span class="nav-text">自定义顶部 top-bar</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Xiaolong"
      src="/images/lion.jpg">
  <p class="site-author-name" itemprop="name">Xiaolong</p>
  <div class="site-description" itemprop="description">Never stop learning</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">45</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/drwna" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;drwna" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



          </div>
        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/Drwna" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2023/01/26/00/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Uniapp 开发记录
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2023-01-26 00:00:00" itemprop="dateCreated datePublished" datetime="2023-01-26T00:00:00+08:00">2023-01-26</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Uniapp-开发记录"><a href="#Uniapp-开发记录" class="headerlink" title="Uniapp 开发记录"></a><strong>Uniapp</strong> 开发记录</h1><h2 id="使用-Vue3-的-setup-语法糖"><a href="#使用-Vue3-的-setup-语法糖" class="headerlink" title="使用 Vue3 的 setup 语法糖"></a>使用 Vue3 的 setup 语法糖</h2><h2 id="配置跨域代理"><a href="#配置跨域代理" class="headerlink" title="配置跨域代理"></a>配置跨域代理</h2><span id="more"></span>

<h3 id="vue2"><a href="#vue2" class="headerlink" title="vue2"></a>vue2</h3><p>使用 HBuilderX 创建的项目</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 文件 mainifest.json</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;h5&quot;</span>: &#123;</span><br><span class="line">		<span class="string">&quot;devServer&quot;</span>: &#123;</span><br><span class="line">			<span class="string">&quot;disableHostCheck&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">			<span class="string">&quot;proxy&quot;</span>: &#123;</span><br><span class="line">				<span class="string">&quot;remote&quot;</span>: &#123;</span><br><span class="line">					<span class="string">&quot;target&quot;</span>: <span class="string">&quot;http://www.xxx.com:8091/&quot;</span>,</span><br><span class="line">					<span class="string">&quot;changeOrigin&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">					<span class="string">&quot;secure&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">					<span class="string">&quot;pathRewrite&quot;</span>: &#123;</span><br><span class="line">						<span class="string">&quot;/remote&quot;</span>: <span class="string">&quot;&quot;</span></span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2023-02-14_14-56-23.png" alt="20230214174116" width="600px"></center>




<p>使用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> url = <span class="string">&#x27;/remote/xxx/xxx&#x27;</span></span><br><span class="line">uni.<span class="title function_">request</span>(&#123;</span><br><span class="line">    <span class="attr">url</span>: url,</span><br><span class="line">    ...</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h3 id="vue3-无ts"><a href="#vue3-无ts" class="headerlink" title="vue3 (无ts)"></a>vue3 (无ts)</h3><p>使用 HBuilderX 创建的项目</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在项目根目录创建 vite.config.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; defineConfig &#125; <span class="keyword">from</span> <span class="string">&#x27;vite&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> uni <span class="keyword">from</span> <span class="string">&#x27;@dcloudio/vite-plugin-uni&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title function_">defineConfig</span>(&#123;</span><br><span class="line">	<span class="attr">plugins</span>: [<span class="title function_">uni</span>()],</span><br><span class="line">	<span class="attr">server</span>: &#123;</span><br><span class="line">		<span class="attr">proxy</span>: &#123;</span><br><span class="line">			<span class="string">&#x27;/remote&#x27;</span>: &#123;</span><br><span class="line">				<span class="attr">target</span>: <span class="string">&quot;http://www.xxx.com:8091/&quot;</span>,</span><br><span class="line">				<span class="attr">changeOrigin</span>: <span class="literal">true</span>,</span><br><span class="line">				<span class="attr">rewrite</span>: <span class="function">(<span class="params">path</span>) =&gt;</span> &#123;</span><br><span class="line">					<span class="keyword">return</span> path.<span class="title function_">replace</span>(<span class="regexp">/^\/remote/</span>, <span class="string">&#x27;&#x27;</span>)</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="App-端无跨域问题，可以直接发送请求"><a href="#App-端无跨域问题，可以直接发送请求" class="headerlink" title="App 端无跨域问题，可以直接发送请求"></a>App 端无跨域问题，可以直接发送请求</h3><h2 id="应用生命周期"><a href="#应用生命周期" class="headerlink" title="应用生命周期"></a>应用生命周期</h2><p><code>App.vue</code>是 uni-app 的主组件，所有页面都是在<code>App.vue</code>下进行切换的，是页面入口文件。<br>但<code>App.vue</code>本身不是页面，这里不能编写视图元素，也就是没有<code>&lt;template&gt;</code>。</p>
<p>这个文件的作用包括：调用应用生命周期函数、配置全局样式、配置全局的存储globalData</p>
<p>应用生命周期仅可在<code>App.vue</code>中监听，在页面监听无效。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;script setup&gt;</span><br><span class="line">	import &#123; upgrade &#125; from &#x27;./util/upgrade.js&#x27;</span><br><span class="line">	import &#123; onShow, onReady, onLaunch &#125; from &#x27;@dcloudio/uni-app&#x27;;</span><br><span class="line"></span><br><span class="line">	// 只能在App.vue里监听应用的生命周期</span><br><span class="line">	onLaunch(() =&gt; &#123;</span><br><span class="line">		console.log(&#x27;App Launch&#x27;)</span><br><span class="line">		upgrade()</span><br><span class="line">	&#125;)</span><br><span class="line">	onShow(() =&gt; &#123;</span><br><span class="line">		console.log(&#x27;App Show&#x27;)</span><br><span class="line">	&#125;)</span><br><span class="line">	onReady(() =&gt; &#123;</span><br><span class="line">		console.log(&#x27;App Ready&#x27;)</span><br><span class="line">	&#125;)</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="条件编译"><a href="#条件编译" class="headerlink" title="条件编译"></a>条件编译</h2><center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/20230214162851.png" alt="20230214174116" width="600px"></center>



<h2 id="版本升级（整包更新）"><a href="#版本升级（整包更新）" class="headerlink" title="版本升级（整包更新）"></a>版本升级（整包更新）</h2><p>函数 upgrade 需要放在 onLaunch 生命周期中</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; useAppInfoStore &#125; <span class="keyword">from</span> <span class="string">&#x27;@/store/appInfo&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">upgrade</span> = <span class="keyword">async</span> (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="comment">// #ifdef APP-PLUS  </span></span><br><span class="line">  <span class="keyword">const</span> remoteInfo = <span class="keyword">await</span> uni.$get(<span class="string">`http://www.xxx.com:8084/appInfo`</span>)</span><br><span class="line">  <span class="keyword">const</span> &#123;<span class="attr">version</span>: remoteVersion, apkName, desc&#125; = remoteInfo</span><br><span class="line">  <span class="comment">// console.log(remoteInfo, remoteInfo.title, remoteInfo.desc)</span></span><br><span class="line">  <span class="keyword">const</span> appInfo = <span class="title function_">useAppInfoStore</span>() <span class="comment">// 获取appInfo</span></span><br><span class="line">  appInfo.<span class="property">upgradeState</span>.<span class="property">version</span> = remoteVersion</span><br><span class="line">  appInfo.<span class="property">upgradeState</span>.<span class="property">desc</span> = desc</span><br><span class="line">  appInfo.<span class="property">newestVersion</span> = remoteVersion <span class="comment">// 更新appInfo中的最新版本号</span></span><br><span class="line"></span><br><span class="line">  plus.<span class="property">runtime</span>.<span class="title function_">getProperty</span>(plus.<span class="property">runtime</span>.<span class="property">appid</span>, <span class="function">(<span class="params">wgtinfo</span>) =&gt;</span> &#123;</span><br><span class="line">    appInfo.<span class="property">currentVersion</span> = wgtinfo.<span class="property">version</span> <span class="comment">// 更新appInfo中的当前版本号</span></span><br><span class="line">    <span class="keyword">const</span> remoteApkUrl = <span class="string">`http://www.safereborn.com:8084/<span class="subst">$&#123;apkName&#125;</span>.apk`</span></span><br><span class="line">    <span class="keyword">if</span>( remoteVersion &gt; wgtinfo.<span class="property">version</span> ) &#123;</span><br><span class="line">      uni.<span class="title function_">showModal</span>(&#123;</span><br><span class="line">        <span class="attr">title</span>: <span class="string">&#x27;版本更新&#x27;</span>,</span><br><span class="line">        <span class="attr">content</span>: <span class="string">&#x27;有新版本，是否更新？&#x27;</span>,</span><br><span class="line">        <span class="attr">confirmText</span>: <span class="string">&#x27;下载更新&#x27;</span>,</span><br><span class="line">        <span class="attr">cancelText</span>: <span class="string">&#x27;取消&#x27;</span>,</span><br><span class="line">        <span class="title function_">success</span>(<span class="params">res</span>) &#123;</span><br><span class="line">          <span class="keyword">if</span> (res.<span class="property">confirm</span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;用户点击确定&#x27;</span>);</span><br><span class="line">            appInfo.<span class="property">upgradeState</span>.<span class="property">isLoading</span> = <span class="literal">true</span></span><br><span class="line">            <span class="keyword">const</span> downloadTask = uni.<span class="title function_">downloadFile</span>(&#123;</span><br><span class="line">              <span class="attr">url</span>: remoteApkUrl,</span><br><span class="line">              <span class="attr">success</span>: <span class="function">(<span class="params">downloadResult</span>) =&gt;</span> &#123;</span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;下载成功, success&#x27;</span>, downloadResult)</span><br><span class="line">                <span class="keyword">if</span> (downloadResult.<span class="property">statusCode</span> === <span class="number">200</span>) &#123;</span><br><span class="line">                  plus.<span class="property">runtime</span>.<span class="title function_">install</span>(downloadResult.<span class="property">tempFilePath</span>, &#123;</span><br><span class="line">                      <span class="attr">force</span>: <span class="literal">false</span></span><br><span class="line">                    &#125;,</span><br><span class="line">                    <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">                      <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;install success...&#x27;</span>);</span><br><span class="line">                      plus.<span class="property">runtime</span>.<span class="title function_">restart</span>();</span><br><span class="line">                    &#125;,</span><br><span class="line">                    <span class="keyword">function</span>(<span class="params">e</span>) &#123;</span><br><span class="line">                      <span class="variable language_">console</span>.<span class="title function_">error</span>(<span class="string">&#x27;install fail...&#x27;</span>);</span><br><span class="line">                    &#125;</span><br><span class="line">                  );</span><br><span class="line">                &#125;</span><br><span class="line">              &#125;,</span><br><span class="line">              <span class="attr">fail</span>: <span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;downloadFile fail&#x27;</span>, err);</span><br><span class="line">              &#125;,</span><br><span class="line">              <span class="title function_">complete</span>(<span class="params"></span>)&#123;</span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;完成！&#x27;</span>)</span><br><span class="line">              &#125;</span><br><span class="line">            &#125;);</span><br><span class="line">            downloadTask.<span class="title function_">onProgressUpdate</span>(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">              <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;下载进度&#x27;</span> + res.<span class="property">progress</span>);</span><br><span class="line">              <span class="keyword">if</span>(res.<span class="property">progress</span> &gt;= <span class="number">100</span>) &#123;</span><br><span class="line">                appInfo.<span class="property">upgradeState</span>.<span class="property">isLoading</span> = <span class="literal">false</span></span><br><span class="line">              &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                appInfo.<span class="property">upgradeState</span>.<span class="property">percent</span> = res.<span class="property">progress</span></span><br><span class="line">              &#125;</span><br><span class="line">            &#125;);</span><br><span class="line">          &#125; <span class="keyword">else</span> <span class="keyword">if</span> (res.<span class="property">cancel</span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;用户点击取消&#x27;</span>);</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="comment">// #endif</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>相关 api :</p>
<ul>
<li><p>[plus.runtime.getProperty()]: <a target="_blank" rel="noopener" href="https://www.html5plus.org/doc/zh_cn/runtime.html#plus.runtime.version">https://www.html5plus.org/doc/zh_cn/runtime.html#plus.runtime.version</a></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">plus.<span class="property">runtime</span>.<span class="title function_">getProperty</span>(plus.<span class="property">runtime</span>.<span class="property">appid</span>, <span class="function">(<span class="params">widgetInfo</span>) =&gt;</span> &#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(widgetInfo.<span class="property">version</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>[uni.downloadFile()]: <a target="_blank" rel="noopener" href="https://uniapp.dcloud.net.cn/api/request/network-file.html#downloadfile">https://uniapp.dcloud.net.cn/api/request/network-file.html#downloadfile</a></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> downloadTask = uni.<span class="title function_">downloadFile</span>(&#123;</span><br><span class="line">    <span class="attr">url</span>: xxx,</span><br><span class="line">    <span class="attr">success</span>: (downloadResult) = &gt;&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;下载成功, success&#x27;</span>, downloadResult)</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">fail</span>: (err) = &gt;&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;downloadFile fail&#x27;</span>, err);</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">complete</span>(<span class="params"></span>) &#123; <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;完成！&#x27;</span>) &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">downloadTask.<span class="title function_">onProgressUpdate</span>((res) = &gt;&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;下载进度&#x27;</span> + res.<span class="property">progress</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></li>
<li><p> [plus.runtim.install(]: <a target="_blank" rel="noopener" href="https://www.html5plus.org/doc/zh_cn/runtime.html#plus.runtime.install">https://www.html5plus.org/doc/zh_cn/runtime.html#plus.runtime.install</a></p>
</li>
</ul>
  <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">plus.<span class="property">runtime</span>.<span class="title function_">install</span>(downloadResult.<span class="property">tempFilePath</span>, &#123;</span><br><span class="line">    <span class="attr">force</span>: <span class="literal">false</span></span><br><span class="line">&#125;,</span><br><span class="line"><span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;install success...&#x27;</span>);</span><br><span class="line">    plus.<span class="property">runtime</span>.<span class="title function_">restart</span>();</span><br><span class="line">&#125;,</span><br><span class="line"><span class="keyword">function</span>(<span class="params">e</span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">error</span>(<span class="string">&#x27;install fail...&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h2 id="配置底部导航-tabbar"><a href="#配置底部导航-tabbar" class="headerlink" title="配置底部导航 tabbar"></a>配置底部导航 tabbar</h2><p>效果：</p>
<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/GIF%202023-2-14%2017-10-47%20(2).gif" alt="20230214174116" width="600px"></center>



<p>配置代码：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">   <span class="string">&quot;tabBar&quot;</span>:&#123;</span><br><span class="line">      <span class="string">&quot;color&quot;</span>:<span class="string">&quot;#707070&quot;</span>,</span><br><span class="line">      <span class="string">&quot;selectedColor&quot;</span>:<span class="string">&quot;#4243e8&quot;</span>,</span><br><span class="line">      <span class="string">&quot;fontSize&quot;</span>:<span class="string">&quot;16px&quot;</span>,</span><br><span class="line">      <span class="string">&quot;backgroundColor&quot;</span>:<span class="string">&quot;#ffffff&quot;</span>,</span><br><span class="line">      <span class="string">&quot;height&quot;</span>:<span class="string">&quot;64px&quot;</span>,</span><br><span class="line">      <span class="string">&quot;iconWidth&quot;</span>:<span class="string">&quot;28px&quot;</span>,</span><br><span class="line">      <span class="string">&quot;list&quot;</span>:[</span><br><span class="line">         &#123;</span><br><span class="line">            <span class="string">&quot;text&quot;</span>:<span class="string">&quot;主页&quot;</span>,</span><br><span class="line">            <span class="string">&quot;pagePath&quot;</span>:<span class="string">&quot;pages/mainPage/mainPage&quot;</span>,</span><br><span class="line">            <span class="string">&quot;iconPath&quot;</span>:<span class="string">&quot;static/icon/home.png&quot;</span>,</span><br><span class="line">            <span class="string">&quot;selectedIconPath&quot;</span>:<span class="string">&quot;static/icon/home-fill.png&quot;</span></span><br><span class="line">         &#125;,</span><br><span class="line">         &#123;</span><br><span class="line">            <span class="string">&quot;text&quot;</span>:<span class="string">&quot;分类&quot;</span>,</span><br><span class="line">            <span class="string">&quot;pagePath&quot;</span>:<span class="string">&quot;pages/category/category&quot;</span>,</span><br><span class="line">            <span class="string">&quot;iconPath&quot;</span>:<span class="string">&quot;static/icon/category.png&quot;</span>,</span><br><span class="line">            <span class="string">&quot;selectedIconPath&quot;</span>:<span class="string">&quot;static/icon/category-fill.png&quot;</span></span><br><span class="line">         &#125;,</span><br><span class="line">         &#123;</span><br><span class="line">            <span class="string">&quot;text&quot;</span>:<span class="string">&quot;图表&quot;</span>,</span><br><span class="line">            <span class="string">&quot;pagePath&quot;</span>:<span class="string">&quot;pages/charts/charts&quot;</span>,</span><br><span class="line">            <span class="string">&quot;iconPath&quot;</span>:<span class="string">&quot;static/icon/chart.png&quot;</span>,</span><br><span class="line">            <span class="string">&quot;selectedIconPath&quot;</span>:<span class="string">&quot;static/icon/chart-fill.png&quot;</span></span><br><span class="line">         &#125;,</span><br><span class="line">         &#123;</span><br><span class="line">            <span class="string">&quot;text&quot;</span>:<span class="string">&quot;我的&quot;</span>,</span><br><span class="line">            <span class="string">&quot;pagePath&quot;</span>:<span class="string">&quot;pages/person/person&quot;</span>,</span><br><span class="line">            <span class="string">&quot;iconPath&quot;</span>:<span class="string">&quot;static/icon/person.png&quot;</span>,</span><br><span class="line">            <span class="string">&quot;selectedIconPath&quot;</span>:<span class="string">&quot;static/icon/person-fill.png&quot;</span></span><br><span class="line">         &#125;</span><br><span class="line">      ]</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>==pages 中需要有对应的页面==</p>
<p><strong>App 端不支持 svg</strong></p>
<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/20230214174116.png" alt="20230214174116" width="600px"></center>



<h2 id="自定义顶部-top-bar"><a href="#自定义顶部-top-bar" class="headerlink" title="自定义顶部 top-bar"></a>自定义顶部 top-bar</h2><p>需要在 pages.json 中配置 <code>    &quot;navigationStyle&quot;: &quot;custom&quot;</code></p>
<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/20230214181046.png" alt="20230214181046" width="600px"></center>

<p>==手机端状态栏高度==</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;view class=&quot;status_bar&quot;&gt; &lt;!-- 这是手机端最顶部的状态栏 --&gt; &lt;/view&gt;</span><br><span class="line">&lt;style&gt;</span><br><span class="line">    .status_bar &#123;</span><br><span class="line">		height: var(--status-bar-height);</span><br><span class="line">		width: 100%;</span><br><span class="line">		background-color: rgba(248, 248, 248, 1);</span><br><span class="line">	&#125;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>














    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/uniapp/" rel="tag"><i class="fa fa-tag"></i> uniapp</a>
              <a href="/tags/vue3/" rel="tag"><i class="fa fa-tag"></i> vue3</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/12/01/00/" rel="prev" title="Canvas">
                  <i class="fa fa-chevron-left"></i> Canvas
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/03/07/00/" rel="next" title="vim + Vscode / WebStorm">
                  vim + Vscode / WebStorm <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2021 – 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Xiaolong</span>
</div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/third-party/search/local-search.js"></script>




  





</body>
</html>
